package com.peakmain.baselibrary.widget;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.andview.refreshview.callback.IHeaderCallBack;
import com.peakmain.baselibrary.R;

/**
 * author ：Peakmain
 * createTime：2019/4/13
 * mail:2726449200@qq.com
 * describe：下拉刷新头部
 */
public class XRefreshViewNewHeader extends LinearLayout implements IHeaderCallBack {
    private ViewGroup mContent;
    private ImageView mRefreshImageView;
    private AnimationDrawable mAnimationDrawable;

    public XRefreshViewNewHeader(Context context) {
        this(context, null);
    }

    public XRefreshViewNewHeader(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public XRefreshViewNewHeader(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        mContent = (ViewGroup) LayoutInflater.from(context).inflate(
                R.layout.xrefreshview_header_new, this);
        mRefreshImageView = findViewById(R.id.refreshprogress);
        mRefreshImageView.setImageResource(R.drawable.load_more_anim);
        mAnimationDrawable = (AnimationDrawable) mRefreshImageView.getDrawable();
    }

    @Override
    public void onStateNormal() {
        mRefreshImageView.setVisibility(View.VISIBLE);
        mRefreshImageView.setImageResource(R.drawable.list_view_pull);
    }

    @Override
    public void onStateReady() {
        mRefreshImageView.setVisibility(View.VISIBLE);
        mRefreshImageView.setImageResource(R.drawable.list_view_release);
    }

    @Override
    public void onStateRefreshing() {
        mRefreshImageView.setVisibility(View.VISIBLE);
        mAnimationDrawable.start();
    }

    @Override
    public void onStateFinish(boolean success) {

    }

    @Override
    public void onHeaderMove(double headerMovePercent, int offsetY, int deltaY) {

    }

    @Override
    public void setRefreshTime(long lastRefreshTime) {

    }

    @Override
    public void hide() {
        mAnimationDrawable.stop();
        setVisibility(View.GONE);
    }

    @Override
    public void show() {
        setVisibility(VISIBLE);
    }

    @Override
    public int getHeaderHeight() {
        return getMeasuredHeight();
    }
}
